package com.demo.adaptdemo;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/3/22.
 */
public class MyAdapter extends BaseAdapter{

    private List<News> data;
    private LayoutInflater inflater;


    public MyAdapter(Context context,List<News> data) {
        this.data=data;
        inflater=LayoutInflater.from(context);
    }

    /**
     * 数据的大小;
     * @return
     */
    @Override
    public int getCount() {
        return data.size();
    }

    /**
     *第position项
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 得到一个可以由自己任意操作其中组件的View
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //第一个参数是布局文件，第二个参数是ViewGroup,如果不是ViewGroup的话用null
        convertView = inflater.inflate(R.layout.news_layout,null);
        ImageView image = (ImageView)convertView.findViewById(R.id.imv);
        TextView title  = (TextView)convertView.findViewById(R.id.tv1);
        TextView content  = (TextView)convertView.findViewById(R.id.tv2);

        image.setBackgroundResource(data.get(position).getImage());
        title.setText(data.get(position).getTitle());
        content.setText(data.get(position).getCxt());

        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("john", data.get(position).getCxt());
            }
        });

        return convertView;
    }
}
